﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;

namespace AlbonWeb.Manager.OrderManager
{
    public partial class Recycle : System.Web.UI.Page
    {
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        private readonly BLL.UserBLL userbll = new BLL.UserBLL();
        private readonly BLL.OrderItemBLL orderitembll = new BLL.OrderItemBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = "where active=0";
                bindRep();
            }

        }
        //绑定订单数据
        protected void bindRep()
        {
            ViewState["limit"] += " and finished=1";
            if (userName.Text.Trim() != "")
            {
                ViewState["limit"] += " and userName like '%" + userName.Text.Trim() + "%'";
            }
            if (refNo.Text.Trim() != "")
            {
                ViewState["limit"] += " and refNo like '%" + refNo.Text.Trim() + "%'";
            }
            AspNetPager1.Visible = true;
            string cmdstr = "select count(*) from OrderInfoView " + ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","OrderInfoView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate desc")
            };
            SqlHelper.BindRepeater(repOrder, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
        }

        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindRep();
        }


        protected void repOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string orderid = ((HiddenField)e.Item.FindControl("hforderid")).Value;
                //显示itemList
                DataTable dtItem = orderitembll.getItemByOrderID(orderid);
                Label itemList = (Label)e.Item.FindControl("itemList");
                string list = "";
                foreach (DataRow dr in dtItem.Rows)
                {
                    list += dr["itemName"].ToString() + "=" + dr["quantity"].ToString() + "<br>";
                }
                itemList.Text = list;
                DataTable dt = orderbll.getOrderByID(orderid);
                //显示OrderStatus（倒计时，buyMe或者订单类别）
                LinkButton lbStatus = (LinkButton)e.Item.FindControl("orderStatus");
                switch (orderbll.orderStatus(orderid))
                {
                    case "unProcessed":
                        {
                            lbStatus.Text = showTime(DateTime.Parse(dt.Rows[0]["createDate"].ToString()));
                            lbStatus.CommandName = "unProcessed";
                            lbStatus.OnClientClick = "return confirm('Put it in work NOW?')";
                            break;
                        }

                    case "BuyMe":
                        {
                            lbStatus.Text = "<img src='/images/buyme.jpg' alt='' />";
                            lbStatus.CommandName = "BuyMe";
                            break;
                        }
                    default:
                        {
                            lbStatus.Text = dt.Rows[0]["className"].ToString();
                            lbStatus.CommandName = "className";
                            lbStatus.Font.Underline = false;
                            lbStatus.ForeColor = System.Drawing.Color.Black;
                            lbStatus.Font.Bold = true;
                            break;
                        }
                }
            }

        }
        //计算时间差
        protected string showTime(DateTime createDate)
        {
            TimeSpan ts = createDate.AddDays(3) - DateTime.Now;
            return ts.Days + "day" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds;
        }
        //查询
        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["limit"] = "where active=0";
            bindRep();
        }

        //删除订单
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            string orderid = ((ImageButton)sender).CommandName.ToString();
            if (orderbll.moveToRecycleBin(orderid))
            {
                bindRep();
            }
        }
    }
}